Daily Exercise 21

Author

Ava Madalinski

Published

April 20, 2025

library(dataRetrieval)
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tsibble)
Registered S3 method overwritten by 'tsibble':
  method               from 
  as_tibble.grouped_df dplyr

Attaching package: 'tsibble'

The following object is masked from 'package:lubridate':

    interval

The following objects are masked from 'package:base':

    intersect, setdiff, union
library(plotly)

Attaching package: 'plotly'

The following object is masked from 'package:ggplot2':

    last_plot

The following object is masked from 'package:stats':

    filter

The following object is masked from 'package:graphics':

    layout
library(feasts)
Loading required package: fabletools
# Example: Cache la Poudre River at Mouth (USGS site 06752260)
poudre_flow <- readNWISdv(siteNumber = "06752260",    # Download data from USGS for site 06752260
                          parameterCd = "00060",      # Parameter code 00060 = discharge in cfs)
                          startDate = "2013-01-01",   # Set the start date
                          endDate = "2023-12-31") |>  # Set the end date
  renameNWISColumns() |>                              # Rename columns to standard names (e.g., "Flow", "Date")
  mutate(Date = yearmonth(Date)) |>                   # Convert daily Date values into a year-month format (e.g., "2023 Jan")
  group_by(Date) |>                                   # Group the data by the new monthly Date
  summarise(Flow = mean(Flow))                       # Calculate the average daily flow for each month
GET:https://waterservices.usgs.gov/nwis/dv/?site=06752260&format=waterml%2C1.1&ParameterCd=00060&StatCd=00003&startDT=2013-01-01&endDT=2023-12-31
streamflow_tsibble <- as_tsibble(poudre_flow, index = "Date")
print(streamflow_tsibble)
# A tsibble: 132 x 2 [1M]
       Date    Flow
      <mth>   <dbl>
 1 2013 Jan   18.1 
 2 2013 Feb   18.0 
 3 2013 Mar    8.21
 4 2013 Apr    5.94
 5 2013 May  333.  
 6 2013 Jun  300.  
 7 2013 Jul   75.6 
 8 2013 Aug   48.8 
 9 2013 Sep 1085.  
10 2013 Oct  146.  
# ℹ 122 more rows
ggplotly(ggplot(streamflow_tsibble, aes(x = Date, y = Flow)) +
  geom_line(color = "steelblue", size = 1) +
  labs(title = "Daily Streamflow", x = "Date", y = "Flow (cms)") +
  theme_minimal())
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
gg_subseries(streamflow_tsibble) +
  labs(title = "Monthly Streamflow Patterns", y = "Flow", x = "Date") +
  theme_minimal()
Plot variable not specified, automatically selected `y = Flow`

The seasons are defined in this subseries graph through similar levels of stream flow. We can see that December, January, and February all have relatively low, similar levels of flow, as is expected in winter. It starts to increase in March and April, as expected with snow melt in the spring and then as it warms up to summer in May and June, the flow levels rise a very large amount. The subseries represent the stream flow for all years, sorted by months so that we can see trends and patterns thorughout the seasons and months.